package com.itheima.d2_byte_stream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
     目标：使用字节流完成对文件的复制操作。
     数据源
     目的地
 */
public class CopyTest5 {
    public static void main(String[] args) throws Exception {
        String source = "D:\\autoML 工具.md";
        String target = "D:\\codes\\copyfile.md";

        long start = System.currentTimeMillis();
        copyFile(source, target);
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
    }

    public static void copyFile(String sourceFilePath, String targetPath) throws IOException {
        // 1. 创建输入流和输出流对象
        FileInputStream fis = new FileInputStream(sourceFilePath);
        FileOutputStream fos = new FileOutputStream(targetPath);

        // 2. 写入数据
        int len = -1;
        // 指定每次读取的字节数
        byte[] bytes = new byte[1024];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }

        // 3. 关闭流
        fis.close();
        fos.close();

    }
}
